# plotly, highcharts - for whole host of different types of charts
# leaflet - for creating interactive maps
# vis.js - visualizing networks and timelines
#leaflet ----
library("tidyverse")
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.5
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library("geosphere")
## Warning: Paket 'geosphere' wurde unter R Version 4.2.2 erstellt
library("leaflet")
## Warning: Paket 'leaflet' wurde unter R Version 4.2.2 erstellt
#library("statesRcontiguous")
library("tidyverse")
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m # Print the map
CapitalsLatitudeLongitude <- read.csv("C:/Users/maciej-sokolowski/Downloads/datasets/CapitalsLatitudeLongitude.csv", sep=";")
CapitalsLatitudeLongitude %>%
leaflet() %>%
addTiles() %>%
addMarkers(label = ~ Capital)
## Assuming "Longitude" and "Latitude" are longitude and latitude, respectively
## Scatter Geo Plots
CapitalsLatitudeLongitude %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers(radius = ~ 0.1,
label = ~Capital)
## Assuming "Longitude" and "Latitude" are longitude and latitude, respectively
## Clustered Scatter Geo Plots
CapitalsLatitudeLongitude %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers(radius = ~ 0.1,
label = ~Capital,
clusterOptions = markerClusterOptions())
## Assuming "Longitude" and "Latitude" are longitude and latitude, respectively
capitals <- read.csv("C:/Users/maciej-sokolowski/Downloads/datasets/capitals_with_locations.csv", sep=",")
capitals$latitude<-capitals$capital.latitude
capitals$longitude<-capitals$capital.longitude
## Size of the ring = population
capitals %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers(radius = ~ country.population/100000000,
label = ~Capital)
## Assuming "longitude" and "latitude" are longitude and latitude, respectively
europe <- CapitalsLatitudeLongitude %>%
filter(CapitalsLatitudeLongitude$Continent=="Europe")
europe %>%
leaflet() %>%
#addTiles() %>%
addProviderTiles(providers$OpenTopoMap) %>%
addMarkers(lng= ~Longitude,
lat= ~Latitude)
europe %>%
leaflet() %>%
#addTiles() %>%
addProviderTiles(providers$NASAGIBS.ViirsEarthAtNight2012) %>%
addMarkers(lng= ~Longitude,
lat= ~Latitude)
library("gapminder")
library("tidyverse")
library("leaflet")
library("sf")
## Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
# Obtained maps from http://www.naturalearthdata.com/downloads/50m-cultural-vectors/
# download.file(
# url = "http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip",
# destfile = "C:/Users/maciej-sokolowski/Downloads/datasets/world-shape-files.zip")
#
# unzip("world-shape-files.zip",
# exdir = "world-shape-files")
world_shapefiles <- read_sf(dsn = "C:/Users/maciej-sokolowski/Downloads/datasets/world-shape-files/")
gapminder_most_recent <- gapminder %>%
mutate_if(is.factor, as.character) %>%
filter(year == max(year)) %>%
select(-continent, -year) %>%
rename(name = country)
gapminder_most_recent$NAME <-gapminder_most_recent$name
gapminder_world <- world_shapefiles %>%
left_join(gapminder_most_recent,by="NAME")
gapminder_world %>%
leaflet() %>%
addTiles() %>%
addPolygons
cont_pall <- colorFactor("Dark2", unique(gapminder_world$CONTINENT))
gapminder_world %>%
leaflet() %>%
addTiles() %>%
addPolygons(weight = 1,
color = ~ cont_pall(CONTINENT))
qpal <- colorNumeric("Blues", gapminder_world$lifeExp, na.color = "#808080")
qpal <- colorBin("Greens", gapminder_world$lifeExp, bins=4)
qpal <- colorBin("Oranges", gapminder_world$lifeExp, bins=5)
qpal <- colorBin("Yellows", gapminder_world$lifeExp, bins=7)
gapminder_world %>%
leaflet() %>%
addTiles() %>%
addPolygons(
stroke = FALSE,
smoothFactor = 0.2,
fillOpacity = 1,
color = ~ qpal(lifeExp),
label = ~ name,
popup = ~ paste("Country:", name, "<br/>", "Life Expectancy:", lifeExp)
) %>%
addLegend(pal = qpal, values = ~lifeExp, opacity = 1,
title = paste0("Life Expectancy <br/>(Gapminder in ",max(gapminder$year, na.rm = TRUE), ")"))
# plotly ----
library("tidyverse")
library("plotly")
## Warning: Paket 'plotly' wurde unter R Version 4.2.2 erstellt
##
## Attache Paket: 'plotly'
##
## Das folgende Objekt ist maskiert 'package:ggplot2':
##
## last_plot
##
## Das folgende Objekt ist maskiert 'package:stats':
##
## filter
##
## Das folgende Objekt ist maskiert 'package:graphics':
##
## layout
# diamonds in a built-in dataset
diamonds %>%
plot_ly(x = ~cut, color = ~clarity) %>%
add_histogram()
#world map
library("gapminder")
gapminder_most_recent <- gapminder %>%
mutate_if(is.factor, as.character) %>%
filter(year == max(year)) %>%
select(-continent, -year) %>%
rename(name = country)
plot_geo() %>%
add_trace(
data = gapminder_most_recent,
z = ~lifeExp,
locations = ~name,
locationmode = "country names"
)
## ggplot2-conversion
load("C:/Users/maciej-sokolowski/Downloads/datasets/british_weather.rdata")
a<-gg_british_weather <- british_weather %>%
mutate(tdiff = tmax - tmin) %>%
group_by(region, mm) %>%
dplyr::summarise(average.tdiff = mean(tdiff, na.rm = TRUE))
## `summarise()` has grouped output by 'region'. You can override using the
## `.groups` argument.
head(a)
## # A tibble: 6 × 3
## # Groups: region [1]
## region mm average.tdiff
## <chr> <fct> <dbl>
## 1 England Jan 5.49
## 2 England Feb 5.94
## 3 England Mar 7.19
## 4 England Apr 8.31
## 5 England May 8.85
## 6 England Jun 8.97
gg_british_weather <- british_weather %>%
mutate(tdiff = tmax - tmin) %>%
group_by(region, mm) %>%
dplyr::summarise(average.tdiff = mean(tdiff, na.rm = TRUE)) %>%
ggplot(aes(
x = mm,
y = average.tdiff,
group = region,
col = region
)) +
geom_line() +
geom_point() +
labs(x = "Month", y = "Mean daily temperature difference") +
ggtitle("Mean daily temperature difference in the UK", subtitle = "Split by region")
## `summarise()` has grouped output by 'region'. You can override using the
## `.groups` argument.
gg_british_weather

ggplotly(gg_british_weather)
# facet grid
gg_facet_weather <- british_weather %>%
mutate(tdiff = tmax - tmin) %>%
group_by(region, mm) %>%
dplyr::summarise(average.tdiff = mean(tdiff, na.rm = TRUE)) %>%
ggplot(aes(
x = mm,
y = average.tdiff,
group = region,
col = region
)) +
geom_line() +
geom_point() +
labs(x = "Month", y = "Mean daily temperature difference") +
ggtitle("Mean daily temperature difference in the UK", subtitle = "Split by region") +
facet_wrap(~region)
## `summarise()` has grouped output by 'region'. You can override using the
## `.groups` argument.
gg_facet_weather

ggplotly(gg_facet_weather)
# Violin
gg_boxplot <- british_weather %>%
mutate(tdiff = tmax - tmin) %>%
group_by(region, mm) %>%
ggplot(aes(
x = region,
y = tdiff,
fill = region
)) + geom_boxplot()
gg_boxplot
## Warning: Removed 987 rows containing non-finite values (stat_boxplot).

ggplotly(gg_boxplot)%>%
layout(margin = list(l = 10))
## Warning: Removed 987 rows containing non-finite values (stat_boxplot).
# space added for category labels to be displayed well
# stacked bar charts
gig_economy_data <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/gig-economy-data.csv")
## Rows: 691 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): country, country_group, occupation
## dbl (1): count
## date (1): timestamp
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Bar chart
gig_economy_data %>%
group_by(country_group) %>%
summarise(jobs.in.country = n()) %>%
plot_ly() %>%
add_trace(x= ~jobs.in.country,
y= ~country_group)
## No trace type specified:
## Based on info supplied, a 'bar' trace seems appropriate.
## Read more about this trace type -> https://plotly.com/r/reference/#bar
## Grouped bar chart
stacked_bar_data <- gig_economy_data %>%
group_by(country_group, occupation) %>%
summarise(jobs.in.occupation = n())
## `summarise()` has grouped output by 'country_group'. You can override using the
## `.groups` argument.
stacked_bar_data %>%
plot_ly() %>%
add_trace(x= ~jobs.in.occupation,
y= ~country_group,
color = ~ occupation,
type = "bar")
stacked_bar_data %>%
plot_ly() %>%
add_trace(x= ~jobs.in.occupation,
y= ~country_group,
color = ~ occupation,
type = "bar") %>%
layout(barmode = "stack")
stacked_bar_data %>%
plot_ly() %>%
add_trace(x= ~jobs.in.occupation,
y= ~country_group,
color = ~ occupation,
type = "bar") %>%
layout(barmode = "stack",
barnorm = "percent")
## Warning: 'layout' objects don't have these attributes: 'barnorm'
## Valid attributes include:
## '_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'smith', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'
stacked_bar_data %>%
plot_ly() %>%
add_trace(x= ~jobs.in.occupation,
y= ~country_group,
color = ~ occupation,
type = "bar") %>%
layout(barmode = "stack",
barnorm = "percent",
margin = list(l = 150))
## Warning: 'layout' objects don't have these attributes: 'barnorm'
## Valid attributes include:
## '_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'smith', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'
#left margin should be 150 pixels
scatter_data <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/scatter_data.csv")
## Rows: 2163 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): type, chromosome
## dbl (2): x, y
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
scatter_data %>%
plot_ly() %>%
add_trace(
x = ~ x,
y = ~ y,
color = ~ type,
type = "scatter",
mode = "markers",
opacity = 0.8
)
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
lines_data <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/lines_data.csv")
## Rows: 35 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): trace
## dbl (2): x, y
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
lines_data %>%
plot_ly() %>%
add_trace(
x = ~ x,
y = ~ y,
color = ~ trace,
type = "scatter",
mode = "lines + markers",
opacity = 0.8
)
library("gapminder")
gapminder_oldest <- gapminder %>%
filter(year == min(year))
gapminder_oldest %>%
plot_ly() %>%
add_trace(
x = ~ gdpPercap,
y = ~ lifeExp,
size = ~ pop,
color = ~ continent
)
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
# change the x axis to the logarithmic scale
gapminder_oldest %>%
plot_ly() %>%
add_trace(
x = ~ gdpPercap,
y = ~ lifeExp,
size = ~ pop,
color = ~ continent
) %>%
layout(xaxis = list(type = "log"))
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
library("tidyverse")
library("gapminder")
## == World Choropleth
world_choropleth_data <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/world-choropleth.csv")
## Rows: 126 Columns: 12
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (9): name, name_long, type, continent, region_un, subregion, sovereignt,...
## dbl (3): lifeExp, pop, gdpPercap
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
world_choropleth_data %>%
plot_geo() %>%
add_trace(
locationmode = "country names",
locations = ~ name,
z = ~ pop
)
## == US Choropleth
us_choropleth_data <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/us-choropleth-data.csv")
## Rows: 98 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): country, sender.city, sender.state
## dbl (4): latitude, longitude, count, items.sent
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
us_choropleth_data %>%
plot_geo() %>%
add_trace(
locationmode = "USA-states",
locations = ~ sender.state,
z = ~ items.sent
) %>%
layout(geo = list(scope = "usa"))
scatter_data <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/scatter_data.csv") %>%
mutate(color = plyr::mapvalues(
type,
from = c("Concordant", "Discordant"),
to = c("#1b9e77", "#fc8d62")
))
## Rows: 2163 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): type, chromosome
## dbl (2): x, y
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
plot(
rep(1, 2),
col = c("#1b9e77", "#fc8d62"),
pch = 19,
cex = 3
)

scatter_data %>%
plot_ly() %>%
add_trace(
x = ~ x,
y = ~ y,
color = ~ type,
#colors = ~ color,
colors = c("#1b9e77", "#fc8d62"),
type = 'scatter',
mode = "markers",
opacity = 0.8
)
## Using factors for scatter plot
scatter_data %>%
mutate(new.column = recode(type, "Concordant" = "Green", "Discordant" = "Orange")) %>%
# mutate(new.column = factor(new.column, levels = c("Green", "Orange"))) %>%
plot_ly() %>%
add_trace(
x = ~ x,
y = ~ y,
color = ~ new.column,
text = ~ type,
colors = c("#1b9e77", "#fc8d62"),
type = 'scatter',
mode = "markers",
opacity = 0.8
)
## Line Charts
lines_data <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/lines_data.csv")
## Rows: 35 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): trace
## dbl (2): x, y
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
lines_data %>%
mutate(new.column = plyr::mapvalues(
trace,
from = c(
"[MQ 16] Local",
"[MQ 16] Thesaurus",
"[MQ 1] Local",
"[MQ 1] Thesaurus",
"Mutect"
),
to = c("light blue",
"dark blue",
"light green",
"dark green",
"pink")
)) %>%
plot_ly() %>%
add_trace(
x = ~ x,
y = ~ y,
color = ~ new.column,
text = ~ trace,
colors = c("#a6cee3", "#1f78b4", "#b2df8a", "#33a02c", "#fb9a99"),
type = 'scatter',
mode = "lines+markers",
opacity = 0.8
) %>%
layout(yaxis = list(type = "log"))
#subplots
library("tidyverse")
library("gapminder")
library("plotly")
gapminder_continents <- gapminder %>%
mutate_if(is.integer, as.numeric) %>%
group_by(continent, year) %>%
mutate(continent.pop = sum(pop),
continent.gdpPercap = mean(gdpPercap),
continent.lifeExp = mean(lifeExp)) %>%
select(-country, -pop, -lifeExp, -gdpPercap) %>%
unique()
paste0("~", "continent.pop")
## [1] "~continent.pop"
as.formula(paste0("~", "continent.pop"))
## ~continent.pop
## == Single Chart
plot_ly(
gapminder_continents
) %>%
add_trace(x = ~year,
color = ~continent,
y = as.formula(paste0("~", "continent.pop")),
type = "scatter",
mode = "line+markers") %>%
layout(yaxis = list(
title = "continent.pop"
))
## == Programmatically generate charts
gapminder_vars <- c("continent.pop", "continent.lifeExp", "continent.gdpPercap")
gap_plots <- lapply(gapminder_vars, function(var) {
plot_ly(
gapminder_continents
) %>%
add_trace(x = ~year,
color = ~continent,
y = as.formula(paste0("~", var)),
type = "scatter",
mode = "line+markers") %>%
layout(yaxis = list(
title = var
))
})
gap_plots[[1]]
gap_plots[[2]]
gap_plots[[3]]
subplot(gap_plots, shareX = TRUE,
nrows = length(gap_plots),
titleY = TRUE) %>%
layout(showlegend = F)
# highcharter ----
library("tidyverse")
library("lubridate")
##
## Attache Paket: 'lubridate'
##
## Die folgenden Objekte sind maskiert von 'package:base':
##
## date, intersect, setdiff, union
library("highcharter")
## Warning: Paket 'highcharter' wurde unter R Version 4.2.2 erstellt
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library("jsonlite")
##
## Attache Paket: 'jsonlite'
##
## Das folgende Objekt ist maskiert 'package:purrr':
##
## flatten
library("gapminder")
world_shapefiles <- fromJSON("C:/Users/maciej-sokolowski/Downloads/datasets/world_geoJSON.json", simplifyVector = FALSE)
gapminder_most_recent <- gapminder %>%
filter(year == max(year))
highchart() %>%
hc_add_series_map(world_shapefiles,
gapminder_most_recent,
value = "pop",
joinBy = c("name", "country"),
name = paste("Population in", max(gapminder_most_recent$year))
) %>%
hc_colorAxis(stops = color_stops()) %>%
hc_legend(valueDecimals = 0) %>%
hc_mapNavigation(enabled = TRUE)
bbv_truth_table <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/bbv_truth_table.csv")
## Rows: 18 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (6): hiv.status, hbsag.status, hcv.rna.status, parv4.igg.status, parv4.d...
## dbl (1): n
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
bbv_truth_table %>%
hchart("treemap",
hcaes(
value = n,
colorValue = n,
name = label
)) %>%
hc_colorAxis(minColor = '#FFFFFF',
maxColor = JS("Highcharts.getOptions().colors[0]")) %>%
hc_tooltip(
formatter = JS(
"function(){
console.log(this);
return this.point.label +
'<br/>' +
'Number of individuals: ' + this.point.value;
}" ))
# bar charts
gig_economy_data <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/gig-economy-data.csv")
## Rows: 691 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): country, country_group, occupation
## dbl (1): count
## date (1): timestamp
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Bar chart
gig_economy_data %>%
group_by(country_group) %>%
summarise(jobs.in.country = n()) %>%
hchart(type = "bar",
hcaes(y= jobs.in.country,
x=country_group))
# Grouped bar chart
stacked_bar_data <- gig_economy_data %>%
group_by(country_group, occupation) %>%
summarise(jobs.in.occupation = n())
## `summarise()` has grouped output by 'country_group'. You can override using the
## `.groups` argument.
stacked_bar_data %>%
hchart(type="bar",
hcaes(y=jobs.in.occupation,
x=country_group,
group = occupation))
stacked_bar_data %>%
hchart(type="bar",
hcaes(y=jobs.in.occupation,
x=country_group,
group = occupation)) %>%
hc_plotOptions(bar = list(stacking = "stack"))
stacked_bar_data %>%
hchart(type="bar",
hcaes(y=jobs.in.occupation,
x=country_group,
group = occupation)) %>%
hc_plotOptions(bar = list(stacking = "percent"))
stacked_bar_data %>%
hchart(type="bar",
hcaes(y=jobs.in.occupation,
x=country_group,
group = occupation)) %>%
hc_plotOptions(bar = list(stacking = "percent")) %>%
hc_tooltip(shared = TRUE)
# interactive scatter charts and bubble charts
## Scatter Plot
scatter_data <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/scatter_data.csv")
## Rows: 2163 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): type, chromosome
## dbl (2): x, y
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
scatter_data %>%
hchart(type = "scatter",
hcaes(group = type))
## Line Charts
lines_data <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/lines_data.csv")
## Rows: 35 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): trace
## dbl (2): x, y
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
lines_data %>%
hchart(type = "line",
hcaes(group = trace,
marker = list(enabled = T)))
lines_data %>%
hchart(type = "line",
hcaes(group = trace,
marker = list(enabled = T))) %>%
hc_yAxis(type = "logarithmic")
## Bubble Chart
library("gapminder")
gapminder_oldest <- gapminder %>%
filter(year == min(year))
gapminder_oldest %>%
hchart(type = "bubble",
hcaes(x = gdpPercap,
y = lifeExp,
size = pop,
group = continent)) %>%
hc_xAxis(type = "logarithmic")
gapminder_oldest %>%
hchart(type = "bubble",
hcaes(x = gdpPercap,
y = lifeExp,
size = pop,
group = continent)) %>%
hc_xAxis(type = "logarithmic") %>%
hc_plotOptions(bubble = list(maxSize = "30%"))
library("tidyverse")
library("lubridate")
online_labour_index <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/online_labour_index.csv")
## Rows: 5376 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): date, occupation, status
## dbl (1): count
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
online_labour_index <- online_labour_index %>%
mutate(date = dmy(date))
dmy("23-4-2017") %>%
class()
## [1] "Date"
highchart(type = "stock") %>%
hc_add_series(data = online_labour_index,
type = "line",
hcaes(x = date,
y = count,
group = occupation)) %>%
hc_legend(enabled = T) %>%
hc_plotOptions(series = list(animation=F))
library("tidyverse")
## Data from https://doi.org/10.6084/m9.figshare.4707316
bbv_truth_table <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/bbv_truth_table.csv")
## Rows: 18 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (6): hiv.status, hbsag.status, hcv.rna.status, parv4.igg.status, parv4.d...
## dbl (1): n
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
bbv_truth_table %>%
hchart(type = "treemap",
hcaes(name=label,
value = n,
colorValue = n)) %>%
hc_colorAxis(minColor = "#FFFFFF",
maxColor = JS("Highcharts.getOptions().colors[0]"))
library("tidyverse")
library("highcharter")
library("RColorBrewer")
gig_economy_data <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/gig-economy-data.csv")
## Rows: 691 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): country, country_group, occupation
## dbl (1): count
## date (1): timestamp
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Use tidyverse to add colours
gig_economy_data <- gig_economy_data %>%
mutate(
country.group.color = plyr::mapvalues(
country_group,
from = c(
"all Africa",
"Australia",
"Canada",
"India",
"other Americas",
"other Asia and Oceania",
"other Europe",
"United Kingdom",
"United States"
),
to = brewer.pal(9, "Paired")
),
occupation.color = plyr::mapvalues(
occupation,
from = unique(gig_economy_data$occupation),
to = brewer.pal(length(unique(
gig_economy_data$occupation
)), "Paired")
)
)
## Bar chart
gig_economy_data %>%
group_by(country_group) %>%
summarise(jobs.in.country = n()) %>%
hchart(type = "bar",
hcaes(
x = country_group,
y = jobs.in.country
))
## Grouped bar chart
gig_economy_data %>%
group_by(country_group, occupation) %>%
summarise(jobs.in.occupation = n()) %>%
hchart(type = "bar",
hcaes(
x = country_group,
y = jobs.in.occupation,
group = occupation
))
## `summarise()` has grouped output by 'country_group'. You can override using the
## `.groups` argument.
gig_economy_data %>%
group_by(country_group, occupation) %>%
summarise(jobs.in.country = n()) %>%
mutate(percentage.of.jobs = 100 * jobs.in.country / sum(jobs.in.country)) %>%
hchart(type = "bar",
hcaes(
x = country_group,
y = percentage.of.jobs,
group = occupation
)) %>%
hc_plotOptions(series = list (stacking = "stack")) %>%
hc_tooltip(shared = T,
valueDecimals=2)
## `summarise()` has grouped output by 'country_group'. You can override using the
## `.groups` argument.
# interactive networks ----
library("tidyverse")
library("visNetwork")
## Warning: Paket 'visNetwork' wurde unter R Version 4.2.2 erstellt
map_nodes <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/nodes.csv")
## Rows: 85 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): country, city, state, location
## dbl (2): latitude, longitude
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
map_edges <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/edges.csv")
## Rows: 77 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (4): from, to, total.items, weight
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
map_nodes <- map_nodes %>%
mutate(id = row_number(),
label = location) %>%
#move id to the first column
select(id, everything())
# use the mapvalues function from plyr to replace the locations with
# the new ids from the mapnodes dataframe
plyr::mapvalues(map_edges$send.location,
from = map_nodes$location,
to = map_nodes$id)
## Warning: Unknown or uninitialised column: `send.location`.
## The following `from` values were not present in `x`: USA Orange NJ, USA Cincinnati OH, USA Newport KY, USA Oskaloosa IA, USA Warsaw IL, USA Koekuk IA?, USA Maisfeld near Oskaloosa, USA Monroe MI, USA Tonawanda NY, USA Oak Grove WI, USA Edwardsville IL, USA New York NY, USA Williamsburg NY, USA Brooklyn NY, USA San Francisco CA, USA Union Hill NY, USA Sonoma CA, DEU Oberweißbach TH, USA Waterloo WI, USA Amherst, Portage Co. WI, USA Minneapolis MN, DEU Gräfenthal TH, DEU Piesau TH, USA Austin TX, USA Brenham TX, DEU Leipzig SA, USA Oliverea NY, USA Trenton NJ, DEU Erfurt TH, USA Perry OK, USA Shamrock OK, USA Midford OK, USA Wellington KS, USA Rochester NY, USA Pittsburgh PN, USA Brownfield TX, USA Plainview TX, USA Lubbock TX, USA Sweetwater TX, USA Spur TX, USA Haskell TX, USA Lamesa TX, USA Big Spring TX, USA Seymour TX, USA Guanah TX, USA Perryto TX, USA Burlington CO, USA Stratton CO, USA Ozona TX, USA Seminole TX, DEU Jena TH, USA Cleveland OH, USA Covington KY, USA Ohio OH, USA O. Folly Island SC, USA Fernandina FL, USA Jacksonville FL, USA St. Augustine FL, USA Blue Ridge Summit PA, USA Chicago IL, USA Orange NJ, USA Warrensville OH, USA Ann Arbor MI, USA Homer OH, USA Wilmington Delaware, USA Columbus OH, USA Wood Ridge NJ, USA Jersey City NJ, USA Princeton NJ, USA New York NY, USA Port Cester NY, USA Rutherford NJ, USA Carlstadt NJ, DEU Sebnitz S, USA Lime Ridge WI, DEU Ribnitz Mecklenburg, DEU Nienhagen Mecklenburg, DEU Neubrunn TH, DEU Limbach SA, DEU Roda TH, DEU Apolda TH, DEU Leisnig SA, DEU Mühlhausen TH, USA Texas various places, DEU Remptendorf TH
## NULL
# map_edges <- map_edges %>%
# mutate (from = plyr::mapvalues(send.location,
# from = map_nodes$location,
# to = map_nodes$id)) %>%
# mutate (to = plyr::mapvalues(receive.location,
# from = map_nodes$location,
# to = map_nodes$id)) %>%
# select(from, to, everything())
visNetwork(map_nodes, map_edges)
library("tidyverse")
library("visNetwork")
map_nodes <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/nodes.csv")
## Rows: 85 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): country, city, state, location
## dbl (2): latitude, longitude
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
map_edges <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/edges.csv")
## Rows: 77 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (4): from, to, total.items, weight
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
map_nodes <- map_nodes %>%
mutate(id = row_number()) %>%
mutate(title = location,
label = city) %>%
select(id, everything())
# map_edges <- map_edges %>%
# mutate(from = plyr::mapvalues(send.location, from = map_nodes$location, to = map_nodes$id),
# to = plyr::mapvalues(receive.location, from = map_nodes$location, to = map_nodes$id)) %>%
# select(from, to, everything())
visNetwork(nodes = map_nodes, edges = map_edges)
visNetwork(nodes = map_nodes, edges = map_edges) %>%
visEdges(arrows = "to")
visNetwork(nodes = map_nodes, edges = map_edges) %>%
visEdges(arrows = "middle")
map_edges <- map_edges %>%
mutate(width = weight/3)
visNetwork(nodes = map_nodes, edges = map_edges) %>%
visEdges(arrows = "middle")
map_nodes <- map_nodes %>%
mutate(color = plyr::mapvalues(country,
from = c("USA","DEU"),
to = c("orange","green")))
visNetwork(nodes = map_nodes, edges = map_edges) %>%
visEdges(arrows = "middle") %>%
visNodes(shape = "square")
#graph layout
library("tidyverse")
library("visNetwork")
library("igraph")
##
## Attache Paket: 'igraph'
##
## Die folgenden Objekte sind maskiert von 'package:lubridate':
##
## %--%, union
##
## Das folgende Objekt ist maskiert 'package:plotly':
##
## groups
##
## Die folgenden Objekte sind maskiert von 'package:dplyr':
##
## as_data_frame, groups, union
##
## Die folgenden Objekte sind maskiert von 'package:purrr':
##
## compose, simplify
##
## Das folgende Objekt ist maskiert 'package:tidyr':
##
## crossing
##
## Das folgende Objekt ist maskiert 'package:tibble':
##
## as_data_frame
##
## Die folgenden Objekte sind maskiert von 'package:stats':
##
## decompose, spectrum
##
## Das folgende Objekt ist maskiert 'package:base':
##
## union
## Data sources: https://www.kaggle.com/mylesoneill/game-of-thrones and manipulated by hand by https://shiring.github.io/
got_nodes <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/GoT_nodes.csv")
## Rows: 208 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (9): name, culture, house, house2, id, label, title, gender, superculture
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
got_edges <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/GoT_edges.csv")
## Rows: 404 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): from, to, type
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
visNetwork(got_nodes, got_edges)
visNetwork(got_nodes, got_edges) %>%
visIgraphLayout()
visNetwork(got_nodes, got_edges) %>%
visIgraphLayout(layout = "layout.star")
visNetwork(got_nodes, got_edges) %>%
visIgraphLayout(layout = "layout_on_grid")
visNetwork(got_nodes, got_edges) %>%
visIgraphLayout(layout = "layout_on_sphere")
# cluster your network
got_nodes <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/GoT_nodes.csv")
## Rows: 208 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (9): name, culture, house, house2, id, label, title, gender, superculture
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
got_edges <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/GoT_edges.csv")
## Rows: 404 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): from, to, type
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
got_nodes <- got_nodes %>%
mutate(group = superculture)
visNetwork(got_nodes,
got_edges) %>%
visIgraphLayout() %>%
visClusteringByGroup(groups = unique(got_nodes$group)) %>%
visLegend(ncol = 2)
visNetwork(got_nodes,
got_edges) %>%
visIgraphLayout() %>%
visClusteringByGroup(groups = unique(got_nodes$group)) %>%
visGroups(groupname = "Unknown CUlture",
color = "red") %>%
visGroups(groupname = "Northern",
color = "purple") %>%
visLegend(useGroups = F,
addNodes = list(list(label = "Unknown Culture", color = "red"
)))
# interactive tables ----
library("DT")
## Warning: Paket 'DT' wurde unter R Version 4.2.2 erstellt
library("tidyverse")
transport_data <- read_csv("C:/Users/maciej-sokolowski/Downloads/datasets/transport_data.csv")
## Rows: 399 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (6): sender.country, sender.city, sender.state, receiver.country, recei...
## dbl (6): sender.latitude, sender.longitude, receiver.latitude, receiver.lon...
## date (1): date
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## =========== Basic table
datatable(transport_data)
## =========== Column Filters
datatable(transport_data,
rownames = FALSE,
filter = list(position = "top"))
## =========== Fixed Columns
## Fixed columns
datatable(
transport_data,
extensions = c("FixedColumns"),
rownames = FALSE,
options = list(
dom = 't',
scrollX = TRUE,
fixedColumns = list(leftColumns = 2)
)
)
## =========== Add/remove columns
datatable(
transport_data,
rownames = FALSE,
extensions = 'Buttons',
options = list(dom = 'Bfrtip', buttons = I('colvis'))
)
## =========== Responsive
## https://rstudio.github.io/DT/extensions.html
datatable(transport_data,
rownames = FALSE,
extensions = c("Responsive"))
transport_data %>%
datatable(rownames = F)
colnames(transport_data)
## [1] "sender.country" "sender.city" "sender.state"
## [4] "sender.latitude" "sender.longitude" "receiver.country"
## [7] "receiver.city" "receiver.state" "receiver.latitude"
## [10] "receiver.longitude" "date" "number.of.items"
## [13] "percent.of.all.items"
#rename columns
gsub("[.]"," ", colnames(transport_data))
## [1] "sender country" "sender city" "sender state"
## [4] "sender latitude" "sender longitude" "receiver country"
## [7] "receiver city" "receiver state" "receiver latitude"
## [10] "receiver longitude" "date" "number of items"
## [13] "percent of all items"
#capitilize first letter
library("stringr")
gsub("[.]"," ", colnames(transport_data)) %>%
str_to_title
## [1] "Sender Country" "Sender City" "Sender State"
## [4] "Sender Latitude" "Sender Longitude" "Receiver Country"
## [7] "Receiver City" "Receiver State" "Receiver Latitude"
## [10] "Receiver Longitude" "Date" "Number Of Items"
## [13] "Percent Of All Items"
pretty_headers <- gsub("[.]"," ", colnames(transport_data)) %>%
str_to_title
transport_data %>%
datatable(rownames = F,
colnames = pretty_headers)
transport_data %>%
datatable(rownames = F,
colnames = pretty_headers,
filter = list(position = "top"),
# we see 50 observations on each page
options = list(pageLength = 50,
#what we see in the search bar
language = list(sSearch = "Filter:")))
library("tidyverse")
library("DT")
library("lubridate")
library("stringr")
my_dates <- ymd(c("2010-01-01",
"2011-02-05",
"2012-04-20",
"2014-05-10"))
my_data <- tibble(
transaction.date = my_dates,
amount.in.dollars = 100:103,
amount.in.pounds = rnorm(4, mean = 100, sd = 15)
)
my_data <- my_data %>%
mutate(conversion = amount.in.dollars / amount.in.pounds)
pretty_headers <- gsub("[.]", " ", colnames(my_data)) %>%
str_to_title()
my_data %>%
datatable(
rownames = FALSE,
colnames = pretty_headers,
filter = list(position = "top"),
options = list(language = list(sSearch = "Filter:"))
) %>%
formatCurrency("amount.in.dollars",
currency = "$") %>%
formatCurrency("amount.in.pounds",
currency = "£",
digits =4,
dec.mark = ",") %>%
formatPercentage("conversion",
digits = 2) %>%
formatDate("transaction.date",
method = "toDateString")
transport_data %>%
datatable(
rownames = FALSE,
colnames = pretty_headers,
extensions = "Responsive",
filter = list(position = "top"),
options = list(language = list(sSearch = "Filter:"))
)
transport_data %>%
datatable(
rownames = FALSE,
colnames = pretty_headers,
extensions = c("Responsive","Buttons"),
filter = list(position = "top"),
options = list(language = list(sSearch = "Filter:"),
buttons = I("colvis"),
dom = "Bfrtip")
#dom = "B")
#dom = "f")
)